package com.shop.controller.admin;

import java.sql.SQLException;

import com.jf.core.BaseController;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Page;
import com.shop.bean.LiuyanBean;

public class LiuyanController extends BaseController{
	/**
	 * 留言列表
	 * */
	public void getLiuyanList(){
		String type = "2";
		String select = "select *";
		String sqlExceptSelect = "from tblliuyan tblly where tblly.lyduixiang=?";
		Integer page = getParaToInt("page");
		if(page == null || "".equals(page)){
			page= 1;
		}
		Page<LiuyanBean> yjs = LiuyanBean.liuyanDao.paginate(page, 55, select,sqlExceptSelect,type);
		setAttr("yjs", yjs);
		render(WEB_INF_URL+"admin/files/listyjs.jsp");
	}
	
	/**
	 * 激活选择的意见
	 * */
	public void jhYjList(){
		final String lycodeList = getPara("lycodeList");
		boolean bool = Db.tx(new IAtom(){
			@Override
			public boolean run() throws SQLException {
				boolean result = true;
				String[] lycodeArr = lycodeList.split("@");
				for(String lycode: lycodeArr){
					result = LiuyanBean.liuyanDao.findById(lycode).set("lystate", 1).update();
					if(!result){
						break;
					}
				}
				return result;
			}
		});
		if(bool){
			renderJson("message","true");
		}else{
			renderJson("message","false");
		}
	} 
	
	/**
	 * 删除选择的意见
	 * */
	public void delYjList(){
		final String lycodeList = getPara("lycodeList");
		boolean bool = Db.tx(new IAtom(){
			@Override
			public boolean run() throws SQLException {
				boolean result = true;
				String[] lycodeArr = lycodeList.split("@");
				for(String lycode: lycodeArr){
					result = LiuyanBean.liuyanDao.deleteById(lycode);
					if(!result){
						break;
					}
				}
				return result;
			}
		});
		if(bool){
			renderJson("message","true");
		}else{
			renderJson("message","false");
		}
	} 
	
	/**
	 * 假删除选择的意见,就是把其状态变为不可用
	 * */
	public void jySelyj(){
		final String lycodeList = getPara("lycodeList");
		boolean bool = Db.tx(new IAtom(){
			@Override
			public boolean run() throws SQLException {
				boolean result = true;
				String[] lycodeArr = lycodeList.split("@");
				for(String lycode: lycodeArr){
					result = LiuyanBean.liuyanDao.findById(lycode).set("lystate", 0).update();
					if(!result){
						break;
					}
				}
				return result;
			}
		});
		if(bool){
			renderJson("message","true");
		}else{
			renderJson("message","false");
		}
	}
	
	/**
	 * 进入详细页面
	 * */
	public void goToDetail(){
		String lycode = getPara("lycode");
		LiuyanBean liuyan = LiuyanBean.liuyanDao.findById(lycode);
		setAttr("liuyan",liuyan);
		render(WEB_INF_URL+"admin/files/lydetail.jsp");
	}
}
